package com.dms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.FileUtils;
import com.dms.common.QueryPageParam;
import com.dms.common.Result;
import com.dms.entity.Menulist;
import com.dms.entity.menutype;
import com.dms.mapper.MenulistMapper;
import com.dms.service.MenulistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dms
 * @since 2023-02-03
 */
@RestController
@RequestMapping( "/menulist")
public class MenulistController {
 @Autowired
 private MenulistService menulistService;
 @Resource
 private MenulistMapper menulistMapper;
@PostMapping("/upload")
public Result upload(MultipartFile file) throws IOException {
      String url= FileUtils.upload(file);
      return Result.suc(url);


}
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Menulist menulist){

        return menulistService.save(menulist)?Result.suc():Result.fail();
    }

//修改
@PostMapping("/update")
public Result update(@RequestBody Menulist menulist){
    return menulistService.updateById(menulist)?Result.suc():Result.fail();
}
//删除
@GetMapping("/del")
public Result del(@RequestParam int id){
    return menulistService.removeById(id)?Result.suc():Result.fail();
}
@PostMapping ("/findtype")
  public List findtype(@RequestBody Menulist menulist){

         QueryWrapper<Menulist> queryWrapper=new QueryWrapper();
         queryWrapper.select("distinct menutype").eq("no",menulist.getNo());

       List re= menulistMapper.selectList(queryWrapper);

      return re;

}
//查询餐馆所有菜单
    @PostMapping ("/findno")
    public List findno(@RequestBody Menulist menulist){

        QueryWrapper<Menulist> queryWrapper=new QueryWrapper();
        queryWrapper.select("*").eq("no",menulist.getNo());

        List re= menulistMapper.selectList(queryWrapper);

        return re;

    }
//根据menutype查询所有数据
@PostMapping("/finmenutypedall")
public List finmenutypedall(@RequestBody Menulist menulist){
    QueryWrapper<Menulist> queryWrapper=new QueryWrapper();
    queryWrapper.select("*").eq("menutype",menulist.getMenutype()).eq("no",menulist.getNo());

    List re= menulistMapper.selectList(queryWrapper);

    return re;
}

    //查询
    @PostMapping("/listpagecc1")
    public Result listpagecc1(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String name=(String) param.get("name");
        String menutype=(String) param.get("menutype");
        String no=(String) param.get("no");



        Page<Menulist> page=new Page();

        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Menulist> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.eq(Menulist::getName, name);
        }
        if(StringUtils.isNotBlank(menutype)){
            lambdaQueryWrapper.like(Menulist::getMenutype,menutype);
        }
        if(StringUtils.isNotBlank(no)){
            lambdaQueryWrapper.eq(Menulist::getNo,no);
        }

        IPage result= menulistService.pageC(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        System.out.println(result.getRecords());
        return Result.suc(result.getRecords(),result.getTotal());
    }
    //查询
    @PostMapping("/listpagecc2")
    public Result listpagecc2(@RequestBody QueryPageParam query){
        HashMap param =query.getParam();
        String no=(String) param.get("no");



        Page<Menulist> page=new Page();

        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Menulist> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(no)){
            lambdaQueryWrapper.eq(Menulist::getNo,no);
        }

        IPage result= menulistService.pageC(page,lambdaQueryWrapper);
        System.out.println("total="+result.getTotal());
        System.out.println(result.getRecords());
        return Result.suc(result.getRecords(),result.getTotal());
    }
}
